<?php

/*
 * Tomato Framework
 * 
 * Copyright 2012-2012 Luax.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @link http://www.tomatoframework.com
 * @link http://www.luaxlou.com
 * @version $Id: CatalogDao.class.php 51 2012-11-05 15:11:39Z luaxcn $
 */

/**
 * 
 * 
 * @package tomatoapp
 * @author Luax <luaxcn@gmail.com>
 */
class CatalogDao {

    public static $pk = 'id';

    public static $modifyfields = 'title,catalog_name,description';
    public static $showfields = 'id,title,catalog_name,description,add_time,update_time';
    public static $listfields = 'id,description,add_time,update_time';

    public static $columns = array(
                    'id' => 'int(11)',
                    'title' => 'varchar(20)',
                    'catalog_name' => 'varchar(20)',
                    'description' => 'varchar(255)',
                    'add_time' => 'datetime',
                    'update_time' => 'datetime',
                    'is_show' => 'tinyint(1)',
            );
    
    public static $autoCreates = array(
                    'add_time' => 'now()',
                    'update_time' => 'now()',
                    'is_show' => '1',
            );
    
    public static $autoUpdates = array(
                    'update_time' => 'now()',
            );

    public function findAll() {
        $keys = self::$listfields;
        $sql = "select 
                    id,
                    title,
                    {$keys}
                from blog_catalog
                limit 10000
            ";

        $sth = Load::db()->prepare($sql);

        $sth->execute();

        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }

    public function find($id) {
        $keys = self::$showfields;

        $sql = "select 
                    id,
                    {$keys} 
                from blog_catalog
                where id = ?
                limit 1
            ";

        $sth = Load::db()->prepare($sql);

        $sth->execute(array($id));

        return $sth->fetch(PDO::FETCH_ASSOC);
    }

    public function save($catalog) {
     

        $keys = self::$modifyfields;

        $values = implode(',',array_fill(0, sizeof(explode(',',$keys)), '?'));
        
        $data = array_values(SqlTemplate::filterFields($keys, $catalog));
        
        
        if(sizeof(self::$autoCreates)>0)
        {
            $keys = $keys.','.implode(',',array_keys(self::$autoCreates));
            
            $values = $values.','.implode(',',array_values(self::$autoCreates));
        }

        $sql = "insert into blog_catalog
                (
                    $keys
                )
                values
                (
                    $values
                )
            ";
       
        $db = Load::db();
        $sth = $db->prepare($sql);

         $result = $sth->execute($data);

 

        if ($result) {
            
            $lastInsertId = Load::db()->lastInsertId();
           
            return $lastInsertId;
        }
        
        return FALSE ;
    }

    public function update($catalog) {

        $t = SqlTemplate::updatePairs(self::$modifyfields, ':');

        
        
        if(sizeof(self::$autoUpdates)>0)
        {
            $arr = array();
            foreach(self::$autoUpdates as $k=>$v)
            {
            
                $arr[] = "{$k}={$v}";
                
             }
           $t = $t.','.implode(',',$arr);      
             
        }
        
        $sql = "update blog_catalog set
                $t
                where id = :id
                limit 1
            ";
              
        $sth = Load::db()->prepare($sql);

        return $sth->execute($catalog);
    }

    public function delete($id) {
        $sql = "delete 
                from blog_catalog
                where id = ?
                limit 1
            ";
        $sth = Load::db()->prepare($sql);

        return $sth->execute(array($id));
    }

}

?>